namespace("sg.canvas");

sg.canvas.Animation = function(obj) {
    this.key = obj.key || "";
    this.image = obj.image.image || null;
    this.frames = [];
    this.isCircular = obj.isCircular || false;
    this.initFrames(obj.frames || []);
    this.__frameIndex = 0;
};
sg.canvas.Animation.prototype.getFrame = function(index) {
    if(index!=null) {
        return this.frames[index];
    }

    var _frame = this.frames[this.__frameIndex];
    if(!_frame._d) _frame._d = 0;
    if(_frame._d == _frame.duration) {
        _frame._d = 0;
        this.__frameIndex++;
    }
    _frame._d++;
    if(this.__frameIndex >= this.frames.length && this.isCircular) {
        this.__frameIndex = 0;
    }
    return _frame;
};
sg.canvas.Animation.prototype.initFrames = function(arr) {
    for(var i=0; i<arr.length; i++) {
        this.frames.push(new sg.canvas.Frame(arr[i]));
    }
};

